package com.weiwu.weiwustore.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiwu.weiwustore.entity.Menus;
import com.weiwu.weiwustore.mapper.MenusMapper;
import com.weiwu.weiwustore.mapper.RoleMapper;
import com.weiwu.weiwustore.mapper.UserMapper;
import com.weiwu.weiwustore.pojo.MenuVo;
import com.weiwu.weiwustore.pojo.MenulistVo;
import com.weiwu.weiwustore.pojo.menuitem;
import com.weiwu.weiwustore.service.MenusService;
import com.weiwu.weiwustore.utils.ObjectFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus> implements MenusService {
    @Autowired
    MenusMapper menusMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public String getMenuByAuthority(Map<String, String> map) {
        // 根据id和email 拿到用户的角色信息  根据角色表中的设置返回菜单栏
        Integer userid = 0;
        if (map.containsKey("id")) {
            if (!Objects.equals(map.get("id"), "")) {
                userid = ObjectFormatUtil.toInteger(map.get("id"));
            }
        }
        String thisusermenus = roleMapper.getMenusByUser(userid);
        String[] lists = thisusermenus.split(",");
        // 屏蔽暂未开发的功能 3 转移申请 5 入库可视化 7 出库可视化
        List<String> liststs = new ArrayList<>();
        liststs.addAll(Arrays.asList(lists));
        return JSONObject.toJSONString(getList(liststs));

    }

    private List<MenuVo> getList(List<String> res) {
        List<MenuVo> ress = new ArrayList<>();
        List<menuitem> menuitems = null;
        // 模块1 申请提交
        menuitems = new ArrayList<>();
        if (res.contains("1")) {
            menuitems.add(new menuitem(3, "icon-cat-skuQuery", "入库申请", "N", "apply/inapply", null));
        }
        if (res.contains("2")) {
            menuitems.add(new menuitem(4, "icon-cat-skuQuery", "出库申请", "N", "apply/outapply", null));
        }
        if (res.contains("1") || res.contains("2")) {
            ress.add(new MenuVo(2, "li-icon-xiangmuguanli", "申请提交",
                    null, null, menuitems));
        }
        // 模块2 入库管理
        menuitems = new ArrayList<>();
        if (res.contains("4")) {
            menuitems.add(new menuitem(21, "icon-order-manage", "入库查询", "N", "in/inselect", null));
            ress.add(new MenuVo(20, "li-icon-dingdanguanli", "入库管理",
                    null, null, menuitems));
        }
        // 模块3 出库管理
        menuitems = new ArrayList<>();
        if (res.contains("6")) {
            menuitems.add(new menuitem(31, "icon-order-manage", "出库查询", "N", "out/outselect", null));
            ress.add(new MenuVo(30, "li-icon-dingdanguanli", "出库管理",
                    null, null, menuitems));
        }
        // 模块4 系统管理
        menuitems = new ArrayList<>();
        if (res.contains("8")){
            menuitems.add(new menuitem(41, "icon-cus-manage", "用户管理", "N", "system/user", null));
        }
        if (res.contains("9")){
            menuitems.add(new menuitem(42, "icon-news-manage", "角色管理", "N", "system/role", null));
        }
        if(res.contains("10")){
            menuitems.add(new menuitem(43, "icon-news-manage", "公告管理", "N", "system/notice", null));
        }
        if (res.contains("8") || res.contains("9") || res.contains("10")) {
            ress.add(new MenuVo(40, "li-icon-xitongguanli", "系统管理",
                    null, null, menuitems));
        }
        // 模块5 仓库管理
        menuitems = new ArrayList<>();
        if(res.contains("11")){
            menuitems.add(new menuitem(61, "icon-provider-manage", "库存查询", "N", "store/inventory", null));
        }
        if(res.contains("12")){
            menuitems.add(new menuitem(62, "icon-provider-manage", "材料管理", "N", "store/material", null));
        }
        if(res.contains("13")) {
            menuitems.add(new menuitem(63, "icon-provider-manage", "仓库管理", "N", "store/store", null));
        }
        if (res.contains("11") || res.contains("12") || res.contains("13")) {
            ress.add(new MenuVo(60, "li-icon-shangchengxitongtubiaozitihuayuanwenjian91", "仓库管理",
                    null, null, menuitems));
        }
        // 模块6 个人中心
        menuitems = new ArrayList<>();
        if(res.contains("14")){
            menuitems.add(new menuitem(101, "icon-provider-manage", "我的信息", "N", "account/account", null));
        }
        if(res.contains("15")){
            menuitems.add(new menuitem(102, "icon-provider-manage", "我的申请", "N", "account/myapply", null));
        }
        if(res.contains("16")){
            menuitems.add(new menuitem(103, "icon-provider-manage", "我的任务", "N", "account/mytask", null));
        }
        if (res.contains("14") || res.contains("15") || res.contains("16")) {
            ress.add(new MenuVo(100, "li-icon-shangchengxitongtubiaozitihuayuanwenjian91", "个人中心",
                    null, null, menuitems));
        }
        return ress;
    }

    @Override
    public List<MenulistVo> getMenulist() {
        List<MenulistVo> res = new ArrayList<>();
        for (Menus m : menusMapper.selectList(null)) {
            res.add(new MenulistVo(m.getMenuid(), m.getMenuname(), isTrue(m.getMenuid())));
        }
        return res;
    }

    @Override
    public List<Integer> getmenusbyroleid(Map<String, String> map) {
        List<Integer> res = new ArrayList<>();
        String[] menu = roleMapper.selectById(ObjectFormatUtil.toInteger(map.get("id")))
                .getMenus().split(",");
        for (int i = 0; i < menu.length; i++) {
            res.add(ObjectFormatUtil.toInteger(menu[i]));
        }
        return res;
    }

    private boolean isTrue(Integer id) {
        if (id.equals(1) || id.equals(2) || id.equals(14) || id.equals(15)) {
            return true;
        }
        return false;
    }

}
